package leetcode.code007;

/**
 * 字符串反转
 * @author CaoYu
 * @date 2021年8月10日
 * @description
 */
public class Solution1 implements ISolution {

	@Override
	public int reverse(int x) {
		String rex = String.valueOf(x);
		StringBuffer res = new StringBuffer();
		for (int i = rex.length() - 1; i > -1; i--) {
			if (i != 0 || rex.charAt(i) != '-') {
				res.append(rex.charAt(i));
			} else {
				res.insert(0, '-');
			}
		}
		try {
			return Integer.valueOf(res.toString());
		} catch (NumberFormatException e) {
			return 0;
		}
	}
}
